package com.guigu.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.mapper.SupplierInfoMapper;
import com.guigu.youxuan.pojo.SupplierInfo;
import com.guigu.youxuan.service.ISupplierInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class SupplierInfoServiceImpl extends ServiceImpl<SupplierInfoMapper, SupplierInfo> implements ISupplierInfoService {

    @Autowired
    private SupplierInfoMapper supplierInfoMapper;

    @Override
    public PageInfo<SupplierInfo> getSupplierList(int pageNo, int pageSize, SupplierInfo supplierInfo) {
        QueryWrapper<SupplierInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", 0);

        // 添加额外的查询条件
        if (supplierInfo.getCompanyName() != null) {
            wrapper.eq("company_name", supplierInfo.getCompanyName());
        }
        if (supplierInfo.getName() != null) {
            wrapper.eq("name", supplierInfo.getName());
        }
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        // 查询语句
        List<SupplierInfo> page = supplierInfoMapper.queryAll(supplierInfo);
        // 封装分页参数
        PageInfo<SupplierInfo> pageInfo = new PageInfo<SupplierInfo>(page);
        return pageInfo;
    }

    @Override
    public PageUtils<SupplierInfo> supplierInfoFuHePage(SupplierInfo supplierInfo, int pageNo, int pageSize) {
        QueryWrapper<SupplierInfo> wrapper=new QueryWrapper<SupplierInfo>();
        //额外加2个条件
        //审核状态: 待审核
        wrapper.eq("check_Tag","S001-1");

        PageHelper.startPage(pageNo,pageSize);
        List<SupplierInfo> supplierInfos=this.list(wrapper);
        PageInfo<SupplierInfo> pageInfo=new PageInfo<SupplierInfo>(supplierInfos);

        //把PageHelper PageUtils
        PageUtils<SupplierInfo> pageUtils=new PageUtils<SupplierInfo>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    @Override
    public void delBySupplierId(SupplierInfo supplierInfo) {
        if (supplierInfo != null && supplierInfo.getSupplierId() != null) {
            // 构建更新的值和条件
            UpdateWrapper<SupplierInfo> updateWrapper = new UpdateWrapper<>();

            // 设置要更新的列和新值
            updateWrapper.set("is_deleted", 1)
                    .eq("supplier_id", supplierInfo.getSupplierId())
                    .eq("is_deleted", 0);;

            // 执行更新操作
            this.update(updateWrapper);
        }
    }
    /**
     * 供应商资料维护带所登录的用户id查询供应商数据
     * @param sysUsersId
     * @return
     */
    @Override
    public SupplierInfo getByIdform(int sysUsersId) {
        SupplierInfo list = supplierInfoMapper.list(sysUsersId);
        return list;
    }


}
